from fastapi import FastAPI, Body
from pydantic import BaseModel
from typing import List
import requests
import json
import os


# 钉钉群号和对应的webhook地址
webhook = "https://oapi.dingtalk.com/robot/send?access_token=12334556"
message = "【河南交警】您的小型汽车,未按规定停放已被记录，请立即驶离，未及时驶离的，将依法予以处罚，谢谢配合!"

# 推送钉钉机器人
def push_message(webhook_url, message):
    headers = {
        "Content-Type": "application/json;charset=UTF-8"
    }
    data = {
        "msgtype": "text",
        "text": {
            "content": message
        }
    }
    response = requests.post(webhook_url, headers=headers, data=json.dumps(data))
    return response.content



# pip install fastapi 
# pip install "uvicorn[standard]" 
# 启动 uvicorn jiaojing:app --host 0.0.0.0  --port 8282 --reload
# Outgoing：http://www.rknn.net:8282/jiaojing

# 定义接收钉钉消息的数据结构
class AtUser(BaseModel):
    dingtalkId: str

class Text(BaseModel):
    content: str

class RequestBody(BaseModel):
    conversationId: str
    atUsers: List[AtUser]
    chatbotUserId: str
    msgId: str
    senderNick: str
    isAdmin: bool
    sessionWebhookExpiredTime: int
    createAt: int
    conversationType: str
    senderId: str
    conversationTitle: str
    isInAtList: bool
    sessionWebhook: str
    text: Text
    robotCode: str
    msgtype: str

app = FastAPI()

@app.get("/jiaojing")
async def receive_message():
    return push_message(webhook, message)



